import { Component, OnInit } from '@angular/core';

import { BladeContainerBase } from "../../../../shared/blade/blade-container-base";
import { Setofkey } from "../../../../entity/setofkey.model";
import { SetofkeyService } from "../../../../service/setofkey.service";
import { Observable } from "rxjs/Observable";
import { Command } from "../../../../shared/toolbar.service";
import { Blade } from "../../../../shared/blade/blade.model";
import { BladeData } from "../../../../shared/blade/blade-data";
import { DialogResult } from "../../../../shared/dialog.service";
import { MatrixStateGroup } from "../../../../entity/matrix-state-group.model";

//矩阵逻辑组
import { MatrixLogicGroup } from "../../../../entity/matrix-logic-group.model";
import { MatrixLogicGroupService } from "../../../../service/matrix-logic-group.service"
import { MatrixLogicGroupEditComponent } from "../matrix-logic-group-edit/matrix-logic-group-edit.component"
import { MatrixLogicListComponent } from "../matrix-logic-list/matrix-logic-list.component"

@Component({
	selector: 'app-matrix-logic-group-list',
	templateUrl: './matrix-logic-group-list.component.html',
	styleUrls: ['./matrix-logic-group-list.component.css']
})
export class MatrixLogicGroupListComponent extends BladeContainerBase<MatrixLogicGroup> {

	public matrixLogicGroups: MatrixLogicGroup[] = [];
	private currentMatrixLogicGroup:MatrixLogicGroup; 
	public original: MatrixLogicGroup[] = [];
	
	constructor(
		private matrixLogicGroupService: MatrixLogicGroupService,
	) { super() }

	ngOnInit(): void {
		this.commands = [
			new Command("add", "添加", "fa fa-plus", "setofkey:insert", blade => this.exeAdd(blade), blade => this.canExeAdd(blade)),
			new Command("edit", "编辑", "fa fa-edit", "setofkey:update", blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
			new Command("delete", "删除", "fa fa-trash-o", "setofkey:delete", blade => this.exeDelete(blade), blade => this.canDelete(blade)),
		];
		super.ngOnInit();
	}

	initData(): any | Observable<any> | Promise<any> {
        this.matrixLogicGroupService.getMatrixLogicGroupsByStateGroupId((this.parentBlade.parentEntity as MatrixStateGroup).stateGroupId)
            .then(res => {
				if(!res){return false};
                this.original = [...res];
				this.matrixLogicGroups = [...this.original];
				this.currentMatrixLogicGroup=this.matrixLogicGroups[0];
				this.showMatrixLogicListBlade(this.currentMatrixLogicGroup);
            })
            .catch(error => this.bladeData.error = error)
            .then(res => this.bladeData.isLoading = false);

    }

	exeAdd(blend: Blade): void {
	    let data: BladeData = new BladeData();
	    data.id = "matrixLogicGroup";
	    data.title = "新建逻辑组";
	    data.isClosingDisabled = false;
	    let createMatrixLogicGroupBlade = this.bladeService.createBlade(MatrixLogicGroupEditComponent, data, true,null,this.currentEntity);
	    this.bladeService.showBlade(createMatrixLogicGroupBlade, this.currentBlade);
	}
	canExeAdd(blend: Blade): boolean {
	    return true;
	}
	exeEdit(blend: Blade): void {
	    if (this.currentEntity != undefined) {
	        let data: BladeData = new BladeData();
	        data.id = "matrixLogicGroupEdit";
	        data.title = this.currentEntity.logicGroupName;
	        let editMatrixLogicGroupBlade = this.bladeService.createBlade(MatrixLogicGroupEditComponent, data, false, this.currentEntity);
	        this.currentBlade.bladeService.showBlade(editMatrixLogicGroupBlade, this.currentBlade);
	    }
	}
	canExeEdit(blend?: Blade): boolean {
	    return this.currentEntity != null;
	}
	exeDelete(blend: Blade): void {
	    let selectedLogicGroups = this.getSelected();
	    if (selectedLogicGroups.length > 0) {
	        this.dialogService
	            .showConfirmationDialog("确认删除", "是否要删除选中项？")
	            .subscribe(dialogResult => {
	                if (dialogResult == DialogResult.Ok) {
	                    this.currentBlade.bladeData.isLoading = true;
	                    this.matrixLogicGroupService.batchDelete(selectedLogicGroups.map(o => o.logicGroupId))
	                        .then(res => {
	                            if (res) {
	                                this.refresh(undefined);
	                            }
	                        })
	                        .catch(error => this.bladeData.error = error)
	                        .then(res => this.bladeData.isLoading = false);
	                }
	            })

	    }

	}
	canDelete(blend?: Blade): boolean {
	    return this.getSelected().length > 0;
	}

	onSeletedChange(matrixLogicGroup: MatrixLogicGroup) {
        if (this.currentBlade.childrenCanDeactivate()) {
            this.showMatrixLogicListBlade(matrixLogicGroup);
        }
	}
	
	//打开当前状态组下的状态列表
	showMatrixLogicListBlade(selectedFarm: MatrixLogicGroup) {
		if (this.currentEntity != selectedFarm
            || this.bladeService.findBlade("showMatrixStateList") == undefined) {

            let data: BladeData = new BladeData();
            data.id = "showMatrixLogicList";
            data.title = selectedFarm.logicGroupName + "状态表达式";
            data.isClosingDisabled = false;
            let matrixLogicListBlade = this.bladeService.createBlade(MatrixLogicListComponent, data, false, null, selectedFarm);
            this.bladeService.showBlade(matrixLogicListBlade, this.currentBlade).then(res => {
                if (res) {
                    this.currentEntity = selectedFarm;
                }
            });
        }
	}

	/**
     * 单选
     * 
     * @param {any} event 
     * @param {any} key 
     * 
     * @memberof MatrixStateGroupListComponent
     */
    onItemCheack(event: HTMLInputElement, key: MatrixLogicGroup) {
        key.isChecked = event.checked;
    }
	
	/**
     * 全选/全不选
     * 
     * @param {HTMLInputElement} event 
     * 
     * @memberof MatrixStateGroupListComponent
     */
    onAllCheack(event: HTMLInputElement) {
        if (this.matrixLogicGroups.length > 0) {
            for (let i = 0; i < this.matrixLogicGroups.length; i++) {
                this.matrixLogicGroups[i].isChecked = event.checked;
            }
        }
    }
    /**
    * 获得选中项
    * 
    * @returns {Setofkey[]} 
    * 
    * @memberof MatrixStateGroupListComponent
    */
    getSelected(): MatrixLogicGroup[] {
        if (this.matrixLogicGroups.length > 0) {
            let filter = this.matrixLogicGroups.filter(ref => ref.isChecked)
            return filter == null ? [] : filter;
        }
        return [];
    }

}



// WEBPACK FOOTER //
// ./src/app/module/basic/matrix-config/matrix-logic-group-list/matrix-logic-group-list.component.ts